using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.UserContrl
{
	public class EmojiItem : Button
	{
		private const int MAX_ELEMENT_NUM = 400;
		private bool isStoryDone;
		private Paragraph sParagraph;
		private ContextMenu menu;
		private MenuItem delItem;
		public EmojiArt mEmojiArt;
		public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(EmojiItem), new PropertyMetadata("", null));
		private string lastTextCache;
		private bool _contentLoaded;
		public event RoutedEventHandler OnDeleteItem;
		public string Text
		{
			get
			{
				return (string)base.GetValue(EmojiItem.TextProperty);
			}
			set
			{
				base.SetValue(EmojiItem.TextProperty, value);
				this.RefreshText();
			}
		}
		public EmojiItem()
		{
			this.InitializeComponent();
			base.SetValue(MMTiltEffect.IsTiltEnabledProperty, true);
			base.set_Opacity(0.0);
			base.add_Loaded(new RoutedEventHandler(this.EmojiItem_Loaded));
		}
		private Storyboard getLoadStoryBoard()
		{
			double num = 0.0;
			double num2 = 1.0;
			TimeSpan timeSpan = TimeSpan.FromSeconds(1.0);
			ExponentialEase exponentialEase = new ExponentialEase();
			exponentialEase.set_EasingMode(2);
			IEasingFunction easingFunction = exponentialEase;
			Storyboard storyboard = new Storyboard();
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_From(new double?(num));
			doubleAnimation.set_To(new double?(num2));
			doubleAnimation.set_Duration(timeSpan);
			doubleAnimation.set_EasingFunction(easingFunction);
			DoubleAnimation doubleAnimation2 = doubleAnimation;
			Storyboard.SetTarget(doubleAnimation2, this);
			Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath(UIElement.OpacityProperty));
			storyboard.get_Children().Add(doubleAnimation2);
			return storyboard;
		}
		private void EmojiItem_Loaded(object sender, RoutedEventArgs e)
		{
			if (this.isStoryDone)
			{
				return;
			}
			this.isStoryDone = true;
			Storyboard loadStoryBoard = this.getLoadStoryBoard();
			loadStoryBoard.Begin();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.sParagraph = (base.GetTemplateChild("sParagraph") as Paragraph);
			this.RefreshText();
		}
		private void CreatMenu()
		{
			if (this.menu != null)
			{
				return;
			}
			this.menu = new ContextMenu();
			this.menu.set_Width(200.0);
			this.menu.set_Background(R.theme.CONTEXTMENU_BACKGROUND_BRUSH);
			this.menu.IsZoomEnabled = false;
			this.delItem = new MenuItem();
			this.delItem.Header = strings.ChatItemDelete;
			this.delItem.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.delItem.Click += new RoutedEventHandler(this.delItem_Click);
			this.menu.get_Items().Add(this.delItem);
			ContextMenuService.SetContextMenu(this, this.menu);
		}
		private void delItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.OnDeleteItem != null)
			{
				this.OnDeleteItem.Invoke(this, e);
			}
		}
		public void InitData(EmojiArt emojiart, RoutedEventHandler ondelete)
		{
			if (emojiart == null)
			{
				return;
			}
			this.Text = emojiart.strEmojiArt;
			this.mEmojiArt = emojiart;
			this.OnDeleteItem = ondelete;
			if (emojiart.nPackID == 1)
			{
				this.CreatMenu();
			}
		}
		private void RefreshText()
		{
			if (this.sParagraph == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.Text))
			{
				this.sParagraph.get_Inlines().Clear();
				return;
			}
			if (this.Text == this.lastTextCache)
			{
				return;
			}
			this.lastTextCache = this.Text;
			this.sParagraph.get_Inlines().Clear();
			List<InLineResult> inLineResult = InlineElementHelper.GetInLineResult(this.lastTextCache);
			int num = 0;
			using (List<InLineResult>.Enumerator enumerator = inLineResult.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					InLineResult current = enumerator.get_Current();
					if (current.mInline != null)
					{
						num++;
						this.sParagraph.get_Inlines().Add(current.mInline);
						if (num >= 400)
						{
							Run run = new Run();
							run.set_Text("......");
							this.sParagraph.get_Inlines().Add(run);
							break;
						}
					}
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/Emoji/EmojiItem.xaml", 2));
		}
	}
}
